package mt.testcomm;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Random;

import mt.Order;
import mt.comm.CommunicationsClient;
import mt.comm.CommunicationsServer;
import mt.comm.ClientSideMessage.Type;
import mt.comm.ServerSideMessage;

import org.junit.Test;
/**
 * 
 * @author Nuno Pardal
 *
 */
public class CommunicationsClientTest {

	private String name = "SOMETHING";
	private Order order = Order.createBuyOrder(name, "EDP", 20, 1);
	private Random random = new Random();
	
	/**
	 * This method verifies if the client is connected or disconnected. It also verifies if the client has a 'name'. 
	 */
	@Test
	public void testConnectionDisconnection() {
		int port = random.nextInt(20);
		CommunicationsClient client = new CommunicationsClient(port);
		CommunicationsServer server = new CommunicationsServer(port);
		
		server.start();
		try {
			client.connect("localhost", name);
			Thread.sleep(500);

			assertTrue(client.isConnected());
			assertTrue(server.clientIsConnected(name));
			
			client.disconnect();

			Thread.sleep(500);
			assertFalse(client.isConnected());
			assertFalse(server.clientIsConnected(name));
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
	}
/**
 * This method verifies if the client has received a message and if that message is of type 'ORDER'.
 */
	@Test
	public void testGetNextMessage() {
		int port = random.nextInt(20);
		CommunicationsClient client = new CommunicationsClient(port);
		CommunicationsServer server = new CommunicationsServer(port);
		
		server.start();
		try {
			client.connect("localhost", name);
			
			server.sendOrder(name, order);
			
			Thread.sleep(500);
			assertTrue(client.hasNextMessage());
			assertEquals(client.getNextMessage().getType(), Type.ORDER);
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * This method verifies if the client has connected to the server and if the server 
	 * has received a message from client and if that message contains a type 'NEW_ORDER'.
	 */
	@Test
	public void testSendOrder() {
		int port = random.nextInt(20);
		CommunicationsClient client = new CommunicationsClient(port);
		CommunicationsServer server = new CommunicationsServer(port);
		
		server.start();
		try {
			client.connect("localhost", name);
			
			client.sendOrder(order);
			
			Thread.sleep(500);
			assertTrue(server.hasNextMessage());
			assertEquals(server.getNextMessage().getType(), ServerSideMessage.Type.CONNECTED);
			assertEquals(server.getNextMessage().getType(), ServerSideMessage.Type.NEW_ORDER);
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
